/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.valohyd.android_utils.tabs;

import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;

/**
 * Represents a single tab.
 * The {@link TabHelper} initializes one of the subclasses of this based
 * on the current platform version, upon call to {@link TabHelper#newTab(String)}()
 */
public class CompatTab implements TabListener{
    /**
     * The native tab object that this {@link CompatTab} acts as a proxy for.
     */
    ActionBar.Tab mTab;
    CompatTabListener mCallback;
    SherlockFragment mFragment;

    final SherlockFragmentActivity mActivity;
    final String mTag;

    public CompatTab(SherlockFragmentActivity activity, String tag) {
        mActivity = activity;
        mTag = tag;
        mTab = activity.getSupportActionBar().newTab();
    }

    public String getTag() {
        return mTag;
    }

    public CompatTab setText(int resId) {
        mTab.setText(resId);
        return this;
    }

    public CompatTab setIcon(int resId) {
        mTab.setIcon(resId);
        return this;
    }

    public CompatTab setTabListener(CompatTabListener callback) {
        mCallback = callback;
        mTab.setTabListener(this);
        return this;
    }

    public CharSequence getText() {
        return mTab.getText();
    }

    public Drawable getIcon() {
        return mTab.getIcon();
    }

    public Object getTab() {
        return mTab;
    }

    public CompatTabListener getCallback() {
        return mCallback;
    }
    
    public CompatTab setFragment(SherlockFragment fragment) {
        mFragment = fragment;
        return this;
    }

    public SherlockFragment getFragment() {
        return mFragment;
    } 
    
    /* 
     * Tablistener
     * */
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction f) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
        ft.disallowAddToBackStack();
        mCallback.onTabReselected(this, ft);
        ft.commit();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction f) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
        ft.disallowAddToBackStack();
        mCallback.onTabSelected(this, ft);
        ft.commit();
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction f) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
        ft.disallowAddToBackStack();
        mCallback.onTabUnselected(this, ft);
        ft.commit();
    }
}